/*=============================================================================

  Library: CFS

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/

#include <QStringList>
#include <QMutexLocker>
#include <QDebug>

#include "CFSPlugin_p.h"
#include "CFSPluginConstants.h"
#include "CFSServiceReference.h"
#include "CFSServiceReference_p.h"
#include "CFSServiceRegistration_p.h"

//----------------------------------------------------------------------------
CFSServiceReference::CFSServiceReference()
  : d_ptr(new CFSServiceReferencePrivate(0))
{

}

//----------------------------------------------------------------------------
CFSServiceReference::CFSServiceReference(const CFSServiceReference& ref)
  : d_ptr(ref.d_ptr)
{
  d_func()->ref.ref();
}

//----------------------------------------------------------------------------
CFSServiceReference::CFSServiceReference(CFSServiceRegistrationPrivate* reg)
  : d_ptr(new CFSServiceReferencePrivate(reg))
{

}

//----------------------------------------------------------------------------
CFSServiceReference::operator bool() const
{
  return getPlugin();
}

//----------------------------------------------------------------------------
CFSServiceReference& CFSServiceReference::operator=(int null)
{
  if (null == 0)
  {
    if (!d_func()->ref.deref())
      delete d_ptr;
    d_ptr = new CFSServiceReferencePrivate(0);
  }
  return *this;
}

//----------------------------------------------------------------------------
CFSServiceReference::~CFSServiceReference()
{
  if (!d_func()->ref.deref())
    delete d_ptr;
}

//----------------------------------------------------------------------------
QVariant CFSServiceReference::getProperty(const QString& key) const
{
  Q_D(const CFSServiceReference);

  QMutexLocker lock(&d->registration->propsLock);

  return d->registration->properties.value(key);
}

//----------------------------------------------------------------------------
QStringList CFSServiceReference::getPropertyKeys() const
{
  Q_D(const CFSServiceReference);

  QMutexLocker lock(&d->registration->propsLock);

  return d->registration->properties.keys();
}

//----------------------------------------------------------------------------
QSharedPointer<CFSPlugin> CFSServiceReference::getPlugin() const
{
  if (d_func()->registration == 0 || d_func()->registration->plugin == 0)
  {
    return QSharedPointer<CFSPlugin>();
  }

  return d_func()->registration->plugin->q_func().toStrongRef();
}

//----------------------------------------------------------------------------
QList<QSharedPointer<CFSPlugin> > CFSServiceReference::getUsingPlugins() const
{
  Q_D(const CFSServiceReference);

  QMutexLocker lock(&d->registration->propsLock);

  return d->registration->dependents.keys();
}

//----------------------------------------------------------------------------
bool CFSServiceReference::operator<(const CFSServiceReference& reference) const
{
  bool sameFw = d_func()->registration->plugin->fwCtx == reference.d_func()->registration->plugin->fwCtx;
  if (!sameFw)
  {
    throw CFSInvalidArgumentException("Can not compare service references "
                                      "belonging to different framework "
                                      "instances.");
  }

  int r1 = getProperty(CFSPluginConstants::SERVICE_RANKING).toInt();
  int r2 = reference.getProperty(CFSPluginConstants::SERVICE_RANKING).toInt();

  if (r1 != r2)
  {
    // use ranking if ranking differs
    return r1 < r2;
  }
  else
  {
    qlonglong id1 = getProperty(CFSPluginConstants::SERVICE_ID).toLongLong();
    qlonglong id2 = reference.getProperty(CFSPluginConstants::SERVICE_ID).toLongLong();

    // otherwise compare using IDs,
    // is less than if it has a higher ID.
    return id2< id1;
  }
}

//----------------------------------------------------------------------------
bool CFSServiceReference::operator==(const CFSServiceReference& reference) const
{
  return d_func()->registration == reference.d_func()->registration;
}

//----------------------------------------------------------------------------
CFSServiceReference& CFSServiceReference::operator=(const CFSServiceReference& reference)
{
  CFSServiceReferencePrivate* curr_d = d_func();
  d_ptr = reference.d_ptr;
  d_ptr->ref.ref();

  if (!curr_d->ref.deref())
    delete curr_d;

  return *this;
}

//----------------------------------------------------------------------------
uint qHash(const CFSServiceReference& serviceRef)
{
  return qHash(serviceRef.d_func()->registration);
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug dbg, const CFSServiceReference& serviceRef)
{
  dbg.nospace() << "Reference for service object registered from "
      << serviceRef.getPlugin()->getSymbolicName() << " " << serviceRef.getPlugin()->getVersion()
      << " (";
  int i = serviceRef.getPropertyKeys().size();
  foreach(QString key, serviceRef.getPropertyKeys())
  {
    dbg.nospace() << key << "=" << serviceRef.getProperty(key).toString();
    if (--i > 0) dbg.nospace() << ",";
  }
  dbg.nospace() << ")";

  return dbg.maybeSpace();
}
